﻿namespace Yom.Template.Model
{
    using System;
    using System.Collections.Generic;

    public class SkinTreeNode<T>
    {
        private readonly List<SkinTreeNode<T>> _Childs;
        public T _Data;
        private readonly SkinTreeNode<T> _Parent;

        public SkinTreeNode()
        {
            this._Childs = new List<SkinTreeNode<T>>();
            this._Data = default(T);
        }

        public SkinTreeNode(T Data, SkinTreeNode<T> Parent)
        {
            this._Childs = new List<SkinTreeNode<T>>();
            this._Data = default(T);
            this._Data = Data;
            this._Parent = Parent;
        }

        public SkinTreeNode<T> AddChild(T Data)
        {
            SkinTreeNode<T> item = new SkinTreeNode<T>(Data, (SkinTreeNode<T>)this);
            this._Childs.Add(item);

            return item;
        }

        public bool HasChilds()
        {
            return (this._Childs.Count != 0);
        }

        public List<SkinTreeNode<T>> Childs
        {
            get
            {
                return this._Childs;
            }
        }

        public T Content
        {
            get
            {
                return this._Data;
            }
            set
            {
                this._Data = value;
            }
        }

        public SkinTreeNode<T> Parent
        {
            get
            {
                return this._Parent;
            }
        }

        public SkinTreeNode<T> Root
        {
            get
            {
                SkinTreeNode<T> parent = (SkinTreeNode<T>)this;

                while (parent.Parent != null)
                    parent = parent.Parent;

                return parent;
            }
        }
    }
}